package com.gylang.sso.controller;

import com.gylang.common.jwt.JwtUser;
import com.gylang.common.jwt.JwtUserDetails;
import com.gylang.common.jwt.JwtUtils;
import com.gylang.common.model.VO.Result;
import com.gylang.common.model.form.LoginForm;
import com.gylang.sso.config.RsaKeyProperties;
import com.gylang.sso.service.SysMenuService;
import com.gylang.sso.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * @author gylang,
 * @data 2019/12/7 15:39,
 * @DESC
 */
@RestController
@RequestMapping("sso")
public class SsoController {
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private RsaKeyProperties properties;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SysMenuService sysMenuService;


    @PostMapping("login")
    public Result login(@RequestBody @Valid LoginForm loginForm, HttpServletRequest request) {

        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(loginForm.getUserName(), loginForm.getPassword());
        // 执行登录认证过程
        Authentication authentication = authenticationManager.authenticate(token);

        Object principal = authentication.getPrincipal();
        if (principal != null && principal instanceof UserDetails) {

            //获取检验成功的用户
            JwtUserDetails userDetails = (JwtUserDetails) principal;
            JwtUser jwtUser = new JwtUser();
            jwtUser.setId(userDetails.getId());
            jwtUser.setUsername(userDetails.getUsername());
            String JTI = JwtUtils.createJTI();
            String resultToken = JwtUtils.generateTokenExpireInMinute(jwtUser, userDetails.getPermission(),JTI, properties.getPrivateKey(), 12 * 60);
            // 缓存登录信息
            String key = "user_token:username:" + jwtUser.getUsername();
            redisTemplate.opsForValue().set(key, JTI,12 * 60, TimeUnit.MINUTES);
            return Result.success(resultToken);
        } else {
            return Result.failure();
        }


    }

    @PostMapping("logout")
    public Result logout() {

        String key = "user_token:username:" + SecurityUtils.getUsername();
        boolean result = redisTemplate.delete(key);
        return Result.auto(result);

    }
}
